package com.cyan.app.workflow.activity.SFC;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseActivity;
import com.cyan.app.workflow.bean.BaseBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.customview.PullDownView;
import com.cyan.app.workflow.customview.ShowTipDialog;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.network.GetRequestParams;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.List;

/**
 * 品号库存
 * Created by DXW on 2016/3/1.
 */
public class ItnbrBatActivity extends BaseActivity {
    public static final String TAG = "ItnbrBatActivity";
    private ListView itnbrBat_lv;
    private PullDownView mPullDownView;
    private List<ItnbrBatBean> mWarehBeans;
    private ItnbrBatAdapter adapter;
    private String mItnbr;
    private ShowTipDialog showdialog = new ShowTipDialog();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.itnbr_bat);
        mItnbr= getIntent().getStringExtra("GdlzLkItnbr");
        titleBar();
        /*
		 * 1.使用PullDownView 2.设置OnPullDownListener 3.从mPullDownView里面获取ListView
		 */
        mPullDownView = (PullDownView) findViewById(R.id.itnbr_bat_lv);
        itnbrBat_lv = mPullDownView.getListView();
        //prossinfo_lv.setOnItemClickListener(this);
        // 隐藏并且禁用头部刷新
        mPullDownView.setHideHeader();
        mPullDownView.setHideFooter();
        AsyncLoader task = new AsyncLoader();
        task.execute(mItnbr,"207");
    }

    public class AsyncLoader extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            showdialog.showDialog(ItnbrBatActivity.this);
        }

        @Override
        protected String doInBackground(String... params) {
            String result = null;
            try{
                JSONObject param = new JSONObject();
                param.put("Itnbr", params[0]);
                result= GetRequestParams.getRequestResult(HttpConfig.HTTP_SFC_SERVER_URL, params[1], param.toString(), getApplicationContext());
            }catch (Exception e){
                Log.e("error", e.toString());
            }
            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            if (s == null) {
                return;
            }
            ItnbrBatLabBean commonBean = new Gson().fromJson(s, ItnbrBatLabBean.class);
            if (commonBean.getCode() < 1) {
                AppUtils.showTip(ItnbrBatActivity.this, commonBean.getMsg());
            } else {
                mWarehBeans = commonBean.getResult();
                startProssThread();
            }
            showdialog.closeDialog();
        }
    }

    private void startProssThread() {
        adapter = new ItnbrBatAdapter(this);
        itnbrBat_lv.setAdapter(adapter);
    }
    //头部信息
    private void titleBar() {
        ((TextView) findViewById(R.id.top_title_info)).setText("品号库位记录");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
    }
    public class ItnbrBatAdapter extends BaseAdapter {
        private LayoutInflater inflater = null;

        public ItnbrBatAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mWarehBeans.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            holder = new ViewHolder();
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER_VERTICAL;
            convertView = inflater.inflate(R.layout.itnbr_bat_list, null);
            holder.itnbr_bat_wareh = (TextView) convertView.findViewById(R.id.itnbr_bat_list_wareh);
            holder.itnbr_bat_fixnr = (TextView) convertView.findViewById(R.id.itnbr_bat_list_fixnr);
            holder.itnbr_bat_onhand = (TextView) convertView.findViewById(R.id.itnbr_bat_list_onhand1);
            holder.itnbr_bat_wareh.setText(mWarehBeans.get(position).getWareh());
            holder.itnbr_bat_fixnr.setText(mWarehBeans.get(position).getFixnr());
            holder.itnbr_bat_onhand.setText(mWarehBeans.get(position).getOnhand1());
            convertView.setTag(holder);

            return convertView;
        }

        class ViewHolder {
            TextView itnbr_bat_wareh;
            TextView itnbr_bat_fixnr;
            TextView itnbr_bat_onhand;
        }
    }
    class ItnbrBatLabBean extends BaseBean {
        private List<ItnbrBatBean> Result;

        public List<ItnbrBatBean> getResult() {
            return Result;
        }

        public void setResult(List<ItnbrBatBean> data) {
            Result = data;
        }
    }
    class ItnbrBatBean {

        private String Itnbr;
        private String Wareh;
        private String Fixnr;
        private String Onhand1;

        public String getWareh() {
            return Wareh;
        }

        public void setWareh(String wareh) {
            Wareh = wareh;
        }

        public String getFixnr() {
            return Fixnr;
        }

        public void setFixnr(String fixnr) {
            Fixnr = fixnr;
        }

        public String getOnhand1() {
            return Onhand1;
        }

        public void setOnhand1(String onhand) {
            Onhand1 = onhand;
        }

        public String getItnbr() {
            return Itnbr;
        }

        public void setItnbr(String itnbr) {
            Itnbr = itnbr;
        }

    }
}
